
** set path
// gl path ""

** open log
cap log close
log using "$path/table_a6.log", replace

// All Counties

use "$path/us_county_dataset.dta", clear

collapse (first) totpop_acs-hispanic, by(fips county state)
gen age_18_29 = age_18_24 + age_25_29
gen age_30_44 = age_30_34 + age_35_39 + age_40_44
gen age_45_64 = age_45_49 + age_50_54 + age_55_59 + age_60_64
gen age_65_up = age_65_69 + age_7074 + age_75_79 + age_80_84 + age_85_up
gen educ_no_hs_deg = educ_nohs + educ_somehs
gen educ_hs_deg = educ_hs
gen educ_some_coll = educ_somecoll + educ_assoc
order educ_ba, after(educ_some_coll)
gen educ_post_grad = educ_post

drop age_18_24-educ_post
drop natam other_race two_race

mean totpop_acs
mat a = e(b)
local totpop_1 = a[1,1]
foreach var of varlist female-educ_post_grad {
mean `var' [aweight = totpop_acs]
mat b = e(b)
local `var'_1 = b[1,1]
}
local N_1 = e(N)

// CoreLogic Counties

use "$path/us_county_dataset.dta", clear
keep if total_records != .

collapse (first) totpop_acs-hispanic, by(fips county state)
gen age_18_29 = age_18_24 + age_25_29
gen age_30_44 = age_30_34 + age_35_39 + age_40_44
gen age_45_64 = age_45_49 + age_50_54 + age_55_59 + age_60_64
gen age_65_up = age_65_69 + age_7074 + age_75_79 + age_80_84 + age_85_up
gen educ_no_hs_deg = educ_nohs + educ_somehs
gen educ_hs_deg = educ_hs
gen educ_some_coll = educ_somecoll + educ_assoc
order educ_ba, after(educ_some_coll)
gen educ_post_grad = educ_post

drop age_18_24-educ_post
drop natam other_race two_race

mean totpop_acs
mat a = e(b)
local totpop_2 = a[1,1]
foreach var of varlist female-educ_post_grad {
mean `var' [aweight = totpop_acs]
mat b = e(b)
local `var'_2 = b[1,1]
}
local N_2 = e(N)

log close

quietly {
	cap log close
	set linesize 255
	log using "$path/county_covariates.tex", text replace
	
	noisily dis "\begin{table}[h]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{County Covariates (ACS 2009-2014)}"
	noisily dis "\label{tab:county_covariates}}"
	noisily dis "\begin{tabular}{lcc}"
	noisily dis "\toprule \toprule"
	noisily dis " Covariate & Mean (All Counties) & Mean (CoreLogic Counties) \\"
	noisily dis "\midrule"
	noisily dis "Total Population & " %6.0fc `totpop_1' " & " %6.0fc `totpop_2' " \\ "
	noisily dis "Percent Age 18 to 29 & " %04.3f `age_18_29_1' " & " %04.3f `age_18_29_2' " \\ "
	noisily dis "Percent Age 30 to 44 & " %04.3f  `age_30_44_1' " & " %04.3f `age_30_44_2' " \\ "
	noisily dis "Percent Age 45 to 64 & "  %04.3f `age_45_64_1' " & " %04.3f `age_45_64_2' " \\ "
	noisily dis "Percent Age 65 and up & "  %04.3f `age_65_up_1' " & " %04.3f `age_65_up_2' " \\ "
	noisily dis "Percent Female & "  %04.3f `female_1' " & " %04.3f `female_2' " \\ "
	noisily dis "Percent White & " %04.3f  `white_1' " & " %04.3f `white_2' " \\ "
	noisily dis "Percent Black & "  %04.3f `black_1' " & " %04.3f `black_2' " \\ "
	noisily dis "Percent Hispanic & "  %04.3f `hispanic_1' " & " %04.3f `hispanic_2' " \\ "
	noisily dis "Percent Asian & "  %04.3f `asian_1' " & " %04.3f `asian_2' " \\ "
	noisily dis "Percent Less than High School & "  %04.3f `educ_no_hs_deg_1' " & " %04.3f `educ_no_hs_deg_2' " \\ "
	noisily dis "Percent High School Degree & " %04.3f  `educ_hs_deg_1' " & " %04.3f `educ_hs_deg_2' " \\ "
	noisily dis "Percent Some College & "  %04.3f `educ_some_coll_1' " & " %04.3f `educ_some_coll_2' " \\ "
	noisily dis "Percent 4-year College Degree & "  %04.3f `educ_ba_1' " & " %04.3f `educ_ba_2' " \\ "
	noisily dis "Percent Post-Graduate & "  %04.3f `educ_post_grad_1' " & " %04.3f `educ_post_grad_2' " \\ "
	noisily dis "Unemployment Rate & "  %04.3f `unemployment_1' " & " %04.3f `unemployment_2' " \\ "
	noisily dis "Household Median Income &" %5.0fc `house_med_inc_1' " & " %5.0fc `house_med_inc_2' " \\ "
	
	noisily dis " \# Counties & " %4.0fc `N_1' " & " %4.0fc `N_2' " \\ "
	noisily dis "\bottomrule \bottomrule"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	
	log off
}
